package com.tang.provider;

import com.tang.entity.User;
import com.tang.remote.OrderRemote;
import com.tang.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by steven on 2018/3/31.
 */
@Api(value = "micro-service-user", tags = {"micro-service-user"})
@Controller
@RefreshScope
public class UserProvider {

    @Resource
    private UserService userService;

    /**
     * 获取用户列表
     *
     * @param page
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取用户列表", httpMethod = "GET", notes = "获取用户列表")
    @GetMapping("/user")
    public ResponseEntity<?> getUserList(
            @RequestParam("page") @ApiParam(required = true, name = "page", value = "当前页") Integer page,
            @RequestParam("pageSize") @ApiParam(required = true, name = "pageSize", value = "每页的大小") Integer pageSize)
            throws Exception {
        return new ResponseEntity<>(userService.getUserList(page, pageSize), HttpStatus.OK);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "添加用户", httpMethod = "POST", notes = "添加用户")
    @PostMapping("/user")
    public ResponseEntity<?> addUser(
            @RequestBody @ApiParam(value = "json格式", name = "用户对象", required = true) User user)
            throws Exception {
        return new ResponseEntity<>(userService.addUser(user),
                HttpStatus.OK);
    }

    /**
     * 测试负载均衡和分布式配置刷新
     */
    @Value("${neo.hello}")
    private String hello;

    @ApiOperation(value = "测试负载均衡和分布式配置刷新", httpMethod = "GET", notes = "测试负载均衡和分布式配置刷新")
    @GetMapping("/user/hello")
    public ResponseEntity<?> hello() throws Exception {
        String hello = "从用户1中获取的hello的值为" + this.hello;
        return new ResponseEntity<>(hello, HttpStatus.OK);
    }

    @Resource
    OrderRemote orderRemote;

    @ApiOperation(value = "用户获取订单名称", httpMethod = "GET", notes = "用户获取订单名称")
    @GetMapping("/getOrder/{name}")
    public ResponseEntity<?> getOrder(@PathVariable("name") @ApiParam(required = true, name = "name", value = "订单名称") String name) {
        return new ResponseEntity<>(orderRemote.getOrder(name), HttpStatus.OK);
    }

}
